package com.k.kc.common.resp;

/**
 * @author kivil
 */
public class PageBase {
    /**
     * 分页标识 ：true or false
     */
    private boolean pageFlag;

    /**
     * 页码
     */
    private Integer pageNo;

    /**
     * 每页数量
     */
    private Integer pageSize;

    /**
     * 总数量
     */
    private Long totalRecordsCount;

    /**
     * 总页数
     */
    private Integer totalPageCount;

    /**
     * 数据库获取数据 - 偏移量
     */
    private Integer offset;

    /**
     * 数据库获取数据 - 数量
     */
    private Integer limit;


    public boolean isPageFlag() {
        return pageFlag;
    }

    public void setPageFlag(boolean pageFlag) {
        this.pageFlag = pageFlag;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalRecordsCount() {
        return totalRecordsCount;
    }

    public void setTotalRecordsCount(Long totalRecordsCount) {
        this.totalRecordsCount = totalRecordsCount;
    }

    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public void startPage(){
        this.pageFlag = true;
        if(this.limit == null){
            if(this.pageSize == null){
                throw new IllegalArgumentException("limit is null and pageSize is null");
            }
            if(this.pageSize<1){
                System.out.println("limit is null pageSize<1 set pageSize=10");
                this.pageSize=10;
            }
            System.out.println("[limit] is null use [pageSize] compute limit");
            this.limit = this.pageSize;
        }
        if(this.offset == null){
            if(this.pageNo==null){
                throw new IllegalArgumentException("[offset] is null and pageNo is null");
            }
            if(this.pageNo<1){
                System.out.println("[offset] is null and pageNo<1 set pageNo=1");
                this.pageNo=1;
            }
            System.out.println("[offset] is null use [pageNo,pageSize] compute offset");
            this.offset = (this.pageNo-1) * this.pageSize;
        }
    }

    public void endPage(Long totalRecordsCount){
        if(totalRecordsCount == null){
            throw  new IllegalArgumentException("totalRecordsCount is not set");
        }
        this.pageFlag = true;
        this.totalRecordsCount = totalRecordsCount;
        this.pageSize = this.getPageSize()==null?this.limit:this.getPageSize();
        this.totalPageCount = totalRecordsCount % getPageSize() == 0 ?
                (int)(totalRecordsCount/getPageSize()):(int)((totalRecordsCount/getPageSize()+1));
    }

}
